package com.ruoyi.ruoyiserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.ruoyiserver.domain.PeObligeeInformtion;
import com.ruoyi.ruoyiserver.domain.PeStudentInformation;
import com.ruoyi.ruoyiserver.domain.PeTabInformation;
import com.ruoyi.ruoyiserver.domain.PeTeacherInformation;
import com.ruoyi.ruoyiserver.mapper.PeStudentInformationMapper;
import com.ruoyi.ruoyiserver.mapper.PeTeacherInformationMapper;
import com.ruoyi.ruoyiserver.mapper.PersonInformationMapper;
import com.ruoyi.ruoyiserver.service.*;
import com.ruoyi.ruoyiserver.vo.PersonInformationVo;
import com.ruoyi.ruoyiserver.vo.TeacherInformationEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.lang.model.element.VariableElement;
import java.util.ArrayList;
import java.util.List;

@Service
public class PersonInformationServiceImpl extends ServiceImpl<PersonInformationMapper, PersonInformationVo> implements PersonInformationService {

    @Autowired
    private PeTeacherInformationMapper teacherInformationMapper;
    @Autowired
    private IPeObligeeInformtionService obligeeInformtionService;
    @Autowired
    private IPeTabInformationService tabInformationService;
    @Autowired
    private IPeStudentInformationService studentInformationService;

    @Override
    public List<PersonInformationVo> getPersonList(PersonInformationVo personInformationVo) {
//        Page<PersonInformationVo> page = new Page<>(personInformationVo.getPage(),personInformationVo.getPageSize());
        List<PersonInformationVo> personInformationVos = new ArrayList<>();
        List<String> idList = new ArrayList<>();
        /*查询教师表*/
        LambdaQueryWrapper<PeTeacherInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(personInformationVo.getIdNumber() != null, PeTeacherInformation::getIdNumber, personInformationVo.getIdNumber());
        if (null != personInformationVo.getName() && !personInformationVo.getName().isEmpty()) {
            queryWrapper.like(PeTeacherInformation::getName, personInformationVo.getName());
        }
        if (null != personInformationVo.getPhone() && !personInformationVo.getPhone().isEmpty()) {
            queryWrapper.eq(PeTeacherInformation::getPhone, personInformationVo.getPhone())
                        .or()
                        .eq(PeTeacherInformation::getHomePhone, personInformationVo.getPhone());
        }
        List<PeTeacherInformation> teacherInformationList = teacherInformationMapper.selectList(queryWrapper);
        if (!teacherInformationList.isEmpty()) {
            for (PeTeacherInformation teacherInformation : teacherInformationList) {
                PersonInformationVo personInformationVo1 = new PersonInformationVo();
                BeanUtils.copyProperties(teacherInformation, personInformationVo1);
                LambdaQueryWrapper<PeTabInformation> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(teacherInformation.getIdNumber() != null, PeTabInformation::getIdNumber, teacherInformation.getIdNumber());
                List<PeTabInformation> list = tabInformationService.list(wrapper);
                if (!list.isEmpty()) {
                    personInformationVo1.setPeTabInformationList(list);
                    idList.add(teacherInformation.getIdNumber());
                }
                personInformationVo1.setType(1);
                personInformationVos.add(personInformationVo1);
            }
        }
        /*查询网签表*/
        if (null != personInformationVo.getIdNumber() || null != personInformationVo.getName()) {
            LambdaQueryWrapper<PeTabInformation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(personInformationVo.getIdNumber() != null, PeTabInformation::getIdNumber, personInformationVo.getIdNumber());
            lambdaQueryWrapper.like(personInformationVo.getName() != null, PeTabInformation::getHomebuyer, personInformationVo.getName());
            List<PeTabInformation> tabInformations = tabInformationService.list(lambdaQueryWrapper);
            if (!tabInformations.isEmpty()) {
                for (PeTabInformation tabInformation : tabInformations) {
                    if (!idList.contains(tabInformation.getIdNumber())) {
                        PersonInformationVo personInformationVo2 = new PersonInformationVo();
                        BeanUtils.copyProperties(tabInformation, personInformationVo2);
                        personInformationVo2.setName(tabInformation.getHomebuyer());
                        personInformationVo2.setBuyHomeAddress(tabInformation.getHomeAddress());
                        personInformationVo2.setType(3);
                        personInformationVos.add(personInformationVo2);
                    }
                }
            }
        }
        /*查询学生表*/
        LambdaQueryWrapper<PeStudentInformation> studentInformationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentInformationLambdaQueryWrapper.eq(personInformationVo.getIdNumber() != null, PeStudentInformation::getIdNumber, personInformationVo.getIdNumber());
        studentInformationLambdaQueryWrapper.like(personInformationVo.getName() != null, PeStudentInformation::getName,personInformationVo.getName());
        studentInformationLambdaQueryWrapper.eq(personInformationVo.getPhone() != null, PeStudentInformation::getPhone,personInformationVo.getPhone()).or()
                .eq(personInformationVo.getPhone() != null, PeStudentInformation::getHomePhone,personInformationVo.getPhone());
        List<PeStudentInformation> studentInformations = studentInformationService.list(studentInformationLambdaQueryWrapper);
        if (!studentInformations.isEmpty()) {
            for (PeStudentInformation studentInformation : studentInformations) {
                PersonInformationVo personInformationVo3 = new PersonInformationVo();
                BeanUtils.copyProperties(studentInformation, personInformationVo3);
                personInformationVo3.setAddress(studentInformation.getHomeAddress());
                personInformationVo3.setType(2);
                personInformationVos.add(personInformationVo3);
            }
        }
        /*查询权利人*/
        LambdaQueryWrapper<PeObligeeInformtion> obligeeInformtionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        obligeeInformtionLambdaQueryWrapper.eq(personInformationVo.getPhone() != null, PeObligeeInformtion::getPhone, personInformationVo.getPhone());
        obligeeInformtionLambdaQueryWrapper.eq(personInformationVo.getIdNumber() != null, PeObligeeInformtion::getIdNumber, personInformationVo.getIdNumber());
        obligeeInformtionLambdaQueryWrapper.like(personInformationVo.getName() != null, PeObligeeInformtion::getAbligee, personInformationVo.getName());
        List<PeObligeeInformtion> obligeeInformtions = obligeeInformtionService.list(obligeeInformtionLambdaQueryWrapper);
        if (!obligeeInformtions.isEmpty()) {
            for (PeObligeeInformtion obligeeInformtion : obligeeInformtions) {
                PersonInformationVo personInformationVo4 = new PersonInformationVo();
                BeanUtils.copyProperties(obligeeInformtion, personInformationVo4);
                personInformationVo4.setName(obligeeInformtion.getAbligee());
                personInformationVo4.setType(4);
                personInformationVos.add(personInformationVo4);
            }
        }
        return personInformationVos;
    }
}
